//
//  LXWebViewController.m
//  绿稀全球购
//
//  Created by 王敏 on 16/7/15.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXWebViewController.h"

#import "LCTabBarController.h"


#import "LXNavigationController.h"

#import <JavaScriptCore/JavaScriptCore.h>

@interface LXWebViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
{
    JSContext * _jsContext;
    
    BOOL _isVideo;
}


@property(strong,nonatomic)VideoPlayer * player;

@end

@implementation LXWebViewController
@synthesize web = _web;

/**
 *  以url初始化
 *
 *  @param url url
 *
 *  @return LXWebViewController
 */
-(instancetype)initWithUrl:(NSString *)url
{
    if(self = [super init])
    {
        if(![url containsString:host]){
            url = [url addHeader:[NSString stringWithFormat:@"%@/",host]];
        }
        self.url = url;
        if([[self.url uppercaseString] containsString:@"VIDEO"]){
            _isVideo = YES;
            self.navigationController.delegate = nil;
        }else{
            _isVideo = NO;
        }
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    self.url = TestVideoUrl;
    if(_isVideo){
        [self setUpPlayer];
        [self forceOrientationLandscape];
        
    }else{
        [self.view addSubview:self.web];
        [self.web loadRequest:request];
    }
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
    LCTabBarController * tbvc = (LCTabBarController*)self.navigationController.tabBarController;
    tbvc.tabBarHidden = YES;
   
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    LCTabBarController * tbvc = (LCTabBarController*)self.navigationController.tabBarController;
    tbvc.tabBarHidden = NO;
    [self forceOrientationPortrait];
    
    [self.player pause];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title= @"";
    if(_isVideo){
        self.navigationController.navigationBar.hidden = YES;
    }else{
        self.navigationController.navigationBar.hidden = NO;
    }
}

-(WKWebView*)web
{
    if(_web == nil)
    {
        
        WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
        WKPreferences * preferences = [[WKPreferences alloc] init];
        
        preferences.javaScriptEnabled = YES;
        // 默认是不能通过JS自动打开窗口的，必须通过用户交互才能打开
        preferences.javaScriptCanOpenWindowsAutomatically = YES;
        
        
         configuration.preferences = preferences;
        
        _web = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
        _web.height = self.view.height - 0;
        _web.y = 0;
        _web.navigationDelegate = self;
        _web.UIDelegate = self;
    }
    return _web;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    
}


-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{

    decisionHandler(WKNavigationActionPolicyAllow);
    
}

-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if(_isVideo){
        [self forceOrientationLandscape];
        [self.player setNeedsLayout];
    }else{
        self.web.frame = self.view.bounds;
        self.web.y = 0;
    }
}

-(BOOL)shouldAutorotate{
    return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;
}

-(void)setUpPlayer{
    
    self.player = (VideoPlayer*) [[UINib nibWithNibName:NSStringFromClass([VideoPlayer class]) bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil].firstObject;
    self.player.videoUrl = self.url;
    [self.view addSubview:self.player];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNoti:) name:@"hiddenNvigationBar" object:nil];
}

-(void)handleNoti:(NSNotification*)noti{
    if([noti.name isEqualToString:@"hiddenNvigationBar"]){
        
        self.navigationController.navigationBar.hidden = [noti.object boolValue];

    }
}


@end
